package cn.ac.siom.query.action;

import cn.ac.siom.query.service.IP_basicService;
import cn.ac.siom.query.util.action.DefaultAction;
import cn.ac.siom.query.vo.P_basic;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("unchecked")
@Controller
@RequestMapping(value = "/home/*")
public class P_basicAction extends DefaultAction {

    @Resource
    private IP_basicService p_basicService;

    /**
     * 激光通讯没用此接口
     */
    @RequestMapping(value = "p_basic_list")
    public void list(HttpServletRequest request, HttpServletResponse response3) {
        response3.setHeader("Access-Control-Allow-Origin", "*");
        try {
            insertAccessinfo(request,"");
            super.printObjectToList(response3, "allP_basic", this.p_basicService.list());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 激光通讯没用此接口
     */
    @ResponseBody
    @RequestMapping(value="findNO_P_basic", method= RequestMethod.GET,produces="application/json;charset=UTF-8")
    public List<P_basic> findNO(HttpServletRequest request, Integer i_id) throws SQLException {
        insertAccessinfo(request,"");
        List<P_basic>  p_basics = (List<P_basic>) this.p_basicService.list_NO(i_id);
        return p_basics;
    }

    /**
     * 激光通讯没用此接口
     */
    @ResponseBody
    @RequestMapping(value="findPage_P_basic", method= RequestMethod.GET,produces="application/json;charset=UTF-8")
    public Map<String, Object> findPage(HttpServletRequest request, Integer rownum, Integer page_id) throws SQLException {
        insertAccessinfo(request,"");
        List<P_basic>  p_basics = (List<P_basic>) this.p_basicService.list_page(rownum,page_id);
        Integer cont=this.p_basicService.list_count();
        Map<String,Object> map = new HashMap<>();
        map.put("p_basics",p_basics);
        map.put("cont",cont);
        map.put("page_id",page_id);
        return map;
    }

    @ResponseBody
    @RequestMapping(value="findPhoto_http_P_basic", method= RequestMethod.GET,produces="application/json;charset=UTF-8")
    public List findPhoto_http(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        insertAccessinfo(request,"首页专家学者图片");
        List<Map<String ,String>> list=new ArrayList<Map<String ,String>>();
        List<P_basic>  p_basics =  this.p_basicService.listHttp_photo();
        for(P_basic p_basic:p_basics) {
            Map<String ,String> map = new HashMap<String ,String>();
            System.out.println(p_basic.toString());
            String http= "http://www.optics.org.cn/pers/pdetail?id="+ String.valueOf(p_basic.getId_basic());
            String name_chi=p_basic.getName_chi();
            String name_eng=p_basic.getName_eng();
            String photo ="http://www.optics.org.cn/web/photo/"+p_basic.getPhoto() ;
            map.put("photo",photo);
            map.put("http",http);
            if (!name_chi.equals("") )
                map.put("name_chi", name_chi);
            else
                map.put("name_eng", name_eng);
            list.add(map);
        }
        return list;
    }

    @Override
    public String getText() {
        return null;
    }
}